package cn.achao.csr.chat.service.strategy.msg;

import cn.achao.csr.api.model.dto.MessageExtraDTO;
import cn.achao.csr.api.model.dto.TextMsgDTO;
import cn.achao.csr.api.model.entity.ChatMsg;
import cn.achao.csr.api.model.enums.MessageTypeEnum;
import cn.achao.csr.api.model.resp.TextMsgResp;
import cn.achao.csr.chat.dao.ChatMsgDao;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Description: 普通文本消息
 * Author: achao
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TextMsgHandler extends AbstractMsgHandler<TextMsgDTO> {

    private final ChatMsgDao msgDao;

    @Override
    MessageTypeEnum getMsgTypeEnum() {
        return MessageTypeEnum.TEXT;
    }

    @Override
    protected void checkMsg(TextMsgDTO body, Long roomId, Long uid) {
        // 校验
        log.info("TextMsgHandler checkMsg");
    }

    @Override
    protected void saveMsg(ChatMsg message, TextMsgDTO body) {
        log.info("TextMsgHandler saveMsg");
        // 保存信息
        ChatMsg msg = new ChatMsg();
        msg.setId(message.getId());
        msg.setContent(body.getContent());

        msgDao.updateById(msg);
    }

    @Override
    public Object showMsg(ChatMsg msg) {
        log.info("TextMsgHandler showMsg");
        TextMsgResp resp = new TextMsgResp();
        resp.setContent(msg.getContent());
        resp.setUrlContentMap(Optional.ofNullable(msg.getExtra()).map(MessageExtraDTO::getUrlContentMap).orElse(null));
        resp.setAtUidList(Optional.ofNullable(msg.getExtra()).map(MessageExtraDTO::getAtUidList).orElse(null));
        // TODO 回复消息
        return resp;
    }

    @Override
    public Object showReplyMsg(ChatMsg msg) {
        log.info("TextMsgHandler showReplyMsg");
        return msg.getContent();
    }

    @Override
    public String showContactMsg(ChatMsg msg) {
        log.info("TextMsgHandler showContactMsg");
        return msg.getContent();
    }

}
